/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filenetapis.pe;

import filenet.vw.api.VWFetchType;
import filenet.vw.api.VWFieldType;
import filenet.vw.api.VWModeType;
import filenet.vw.api.VWParameter;
import filenet.vw.api.VWQueue;
import filenet.vw.api.VWQueueQuery;
import filenet.vw.api.VWStepElement;
import java.util.ArrayList;
import java.util.List;
import filenetapis.entities.Fields;
import filenetapis.entities.Workitems;

/**
 *
 * @author Administrator
 */
public class GetWorkItems extends PEHandler {

    public String[][] getWorkItems(String username) {
        String[][] workClassNames = null;
        try {
            this.openPEConn();

            String queueName = "Inbox";
            // Retrieve the Queue
            VWQueue queue = this.getMyPESession().getQueue(queueName);
            // Set Query Parameters

            // Query Flags and Type to retrieve Step Elements
            int queryFlags = VWQueue.QUERY_READ_LOCKED;
            int queryType = VWFetchType.FETCH_TYPE_STEP_ELEMENT;
            VWQueueQuery queueQuery = queue.createQuery(null, null, null, queryFlags, null, null, queryType);
            // Get an individual Step Element
            int i = 0;
            while (queueQuery.hasNext()) {
                VWStepElement stepElement = (VWStepElement) queueQuery.next();
                workClassNames[i][1] = (String) stepElement.getParameter("F_Subject").getValue();
                workClassNames[i][2] = (String) stepElement.getParameter("SequenceNumber").getValue();

            }

            this.closePEConn();

        } catch (Exception vwe) {
            logger.error(vwe);
        } finally {

            return workClassNames;
        }
    }

    public List<Workitems> getWorkParametersandResponses() {
        List<Fields> fieldsList = null;
        List<String> responses = null;
        List<Workitems> workitemsLst = new ArrayList<Workitems>();
        try {
            this.openPEConn();
            logger.info("Test");
            String queueName = "Inbox";
            // Retrieve the Queue
            VWQueue queue = this.getMyPESession().getQueue(queueName);
            // Set Query Parameters

            int queryFlags = VWQueue.QUERY_READ_LOCKED;
            int queryType = VWFetchType.FETCH_TYPE_STEP_ELEMENT;
            VWQueueQuery queueQuery = queue.createQuery(null, null, null, queryFlags, null, null, queryType);

            while (queueQuery.hasNext()) {
                responses = new ArrayList<String>();
                fieldsList = new ArrayList<Fields>();

                VWStepElement stepElement = (VWStepElement) queueQuery.next();

                VWParameter[] parameters = stepElement.getParameters(VWFieldType.BASIC_FIELD_TYPES,
                        VWStepElement.FIELD_USER_DEFINED);
                if (parameters != null) {

                    for (int i = 0; i < parameters.length; i++) {
                        String parameterName = parameters[i].getName();
                        String parameterType = VWFieldType.getLocalizedString(parameters[i].getFieldType());
                        String parameterMode = VWModeType.getLocalizedString(parameters[i].getMode());
                        String parameterValue = parameters[i].getStringValue();
                        fieldsList.add(new Fields(parameterName, parameterType, parameterValue, parameterMode));

                    }

                }

                String[] stepResponses = stepElement.getStepResponses();
                if (stepResponses != null) {
                    int len = stepResponses.length;
                    for (int j = 0; j < len; j++) {

                        responses.add(stepResponses[j]);
                    }
                }

                workitemsLst.add(new Workitems(fieldsList, responses));

            }
            this.closePEConn();
        } catch (Exception vwe) {
            logger.error(vwe);
        } finally {
            return workitemsLst;
        }

    }
}
